package com.xbongbong.sys.model.impl;

import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.sys.domain.dao.FeeDao;
import com.xbongbong.sys.domain.entity.FeeEntity;
import com.xbongbong.sys.model.FeeModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @description:
 * @Author: hongxiao
 * @date: 2019-01-14 20:33
 */
@Service("feeModel")
public class FeeModelImpl implements FeeModel {

    @Resource
    private FeeDao feeDao;

    @Override
    public Integer insert(FeeEntity entity){
        long now = DateUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);

        return feeDao.insert(entity);
    }

    @Override
    public Integer update(FeeEntity entity){
        entity.setUpdateTime((long)DateUtil.getInt());

        return feeDao.update(entity);
    }

    @Override
    public Integer save(FeeEntity entity){

        if(entity.getId() == null || Objects.equals(0L, entity.getId())) {
            return feeDao.insert(entity);
        }
        return feeDao.update(entity);
    }

    @Override
    public Integer deleteByKey( Long key){
        return feeDao.deleteByKey(key);
    }

    @Override
    public FeeEntity getByKey( Long key){
        return feeDao.getByKey(key);
    }

    @Override
    public List<FeeEntity> findEntitys(Map<String ,Object> param){
        return feeDao.findEntitys(param);
    }

    @Override
    public Integer getEntitysCount(Map<String ,Object>  param){
        return feeDao.getEntitysCount(param);
    }

    @Override
    public FeeEntity getEntitysByFeeNameAndUerNum(Integer userNum,String feeName){
        Map<String,Object> param = new HashMap<>(16);
        param.put("primaryNum", userNum);
        param.put("name", feeName);
        List<FeeEntity> feeEntityList=this.findEntitys(param);
        return feeEntityList.size()==0?null:feeEntityList.get(0);
    }

    @Override
    public FeeEntity getEntitysByFeeName(String feeName){
        Map<String,Object> param = new HashMap<>(16);
        param.put("name", feeName);
        List<FeeEntity> feeEntityList=this.findEntitys(param);
        return feeEntityList.size()==0?null:feeEntityList.get(0);
    }

    @Override
    public List<FeeEntity> getFeeList(){
        Map<String, Object> param = new HashMap<>(16);
        param.put("orderByStr ", " STATUS DESC ,NAME");
        param.put("del", "0");
        param.put("start", 0);
        param.put("pageNum", 50);
        List<FeeEntity> feeEntityList = findEntitys(param);
        return feeEntityList;
    }

    /**
     * 根据id list获取套餐列表
     *
     * @param feeIdIn 套餐ID队列
     * @return
     * @author youli.chen
     */
    @Override
    public List<FeeEntity> getFeeListByIdIn(List<Long> feeIdIn) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("del", DelEnum.NORMAL.getDel());
        param.put("idIn", feeIdIn);
        // 批量外呼套餐
        param.put("type", 3);
        List<FeeEntity> feeEntityList = findEntitys(param);
        return feeEntityList;
    }

    @Override
    public List<FeeEntity> getByType(int type) {
        return feeDao.getByType(type);
    }
}
